<template>
  <view>
    <view class="benben-index-anchor-wrapper" :id="guid(10, false)" :style="[wrapperStyle]">
      <view class="benben-index-anchor" :class="[active !== false ? 'benben-index-anchor--active' : '']" :style="[customAnchorStyle]">
        <slot v-if="useSlot" />
        <block v-else>
          <text>{{ index }}</text>
        </block>
      </view>
    </view>
  </view>
</template>

<script>
import { myMinxin } from './mixin_index.js'
export default {
  mixins: [myMinxin],
  name: 'index-letter',
  props: {
    useSlot: {
      type: Boolean,
      default: false,
    },
    index: {
      type: String,
      default: '',
    },
    customStyle: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      active: false,
      wrapperStyle: {},
      anchorStyle: {},
    }
  },
  created() {
    this.parent = false
  },
  mounted() {
    this.parent = this.parentFn.call(this, 'index-list')
    if (this.parent) {
      this.parent.children.push(this)
      this.parent.updateData()
    }
  },
  computed: {
    customAnchorStyle() {
      return Object.assign(this.anchorStyle, this.customStyle)
    },
  },
  beforeDestroy() {
    if (this.parent) {
      this.parent.children.splice(this.parent.children.indexOf(this), 1)
      this.parent.updateData()
    }
  },
  methods: {},
}
</script>

<style lang="scss" scoped>
// 定义混入指令，用于在非nvue环境下的flex定义，因为nvue没有display属性，会报错
@mixin vue-flex($direction: row) {
  /* #ifndef APP-NVUE */
  display: flex;
  flex-direction: $direction;
  /* #endif */
}

.benben-index-anchor {
  box-sizing: border-box;
  padding: 14rpx 24rpx;
  color: var(--city-letter-color, #606266);
  width: 100%;
  font-weight: 500;
  font-size: 28rpx;
  line-height: 1.2;
  background-color: var(--city-letter-bg, #fff);
}

.benben-index-anchor--active {
  right: 0;
  left: 0;
  color: var(--city-letter-active-color, #2979ff);
  background-color: var(--city-letter-active-bg, #fff);
}
</style>
